class ListNode {
    val: number
    next: ListNode | null
    constructor(val?: number, next?: ListNode | null) {
        this.val = (val === undefined ? 0 : val)
        this.next = (next === undefined ? null : next)
    }
}
/**
 * https://leetcode-cn.com/problems/reverse-linked-list/submissions/
 * 翻转链表
 * @param head 
 */
const reverseList = (head: ListNode | null): ListNode | null => {
    // 不需要翻转
    if (head === null || head.next === null) return head;

    // 一直找到最后一个节点
    const newHead = reverseList(head.next);

    // 调整顺序
    head.next.next = head;
    head.next = null
    
    return newHead;
};



const reverseList2 = (head: ListNode | null): ListNode | null => {
    // 不需要翻转
    if (head === null || head.next === null) return head;

    // 新头 
    let newHead = null

    while (head !== null) {
        const n: ListNode | null = head.next;

        // 调整新头
        head.next = newHead;
        newHead = head

        head = n;
    }

    return newHead;
};

export {}